package com.jie.lipassword;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jie.lipassword.db.DbContect;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class PasswordAddActivity extends AppCompatActivity{
    private RelativeLayout password_add_title;
    private String type_name;
    private Button password_add_button;
    private EditText user;
    private EditText password;
    private EditText note;


    DbContect helper;
    private SQLiteDatabase db;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_add);
        type_name = getIntent().getStringExtra("type_name");

        dbcontext();
        set_init();
    }

    private void set_init() {
        password_add_title = findViewById(R.id.password_add_title);
        password = findViewById(R.id.password);
        user = findViewById(R.id.user);
        note = findViewById(R.id.note);
        password_add_button = findViewById(R.id.password_add_button);


        FindTitle.setTitle(this,password_add_title,true,false,"添加"+type_name+"账号密码");
        password_add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断账号密码是否为空
                if (!user.getText().toString().equals("") && !password.getText().toString().equals("")){
                    //不为空
                    Cursor cursor = db.query("PasswordParameters",null,"type_name=? and account=?",new String[]{type_name,user.getText().toString()},null,null,null);
                    while(cursor.moveToNext()){
                        @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex("id"));
                        @SuppressLint("Range") String account = cursor.getString(cursor.getColumnIndex("account"));
                        Log.i("PasswordAdd","result: id="  + id +" account: " + account);
                    }
                    if (cursor != null && cursor.moveToFirst()){
                        //账号已存在
                        Toast.makeText(view.getContext(), "账号已存在", Toast.LENGTH_SHORT).show();
                        // 关闭游标，释放资源
                        cursor.close();
                    }else {
                        //账号不存在
                        ContentValues values = new ContentValues();
                        values.put("type_name",type_name);
                        values.put("account",user.getText().toString());
                        values.put("password",password.getText().toString());
                        values.put("note",note.getText().toString());
                        db.insert("PasswordParameters", null, values);
                        // 关闭游标，释放资源
                        cursor.close();
                        finish();
                    }
                }else {
                    //为空
                    Log.i("TAG", "空 ");
                }
            }
        });
    }

    private void dbcontext(){
        helper = new DbContect(this);
        db = helper.getWritableDatabase();
    }
}
